package main

import "fmt"

func test1(user map[string]map[string]int, name string) {
	if user[name] == nil {
		user[name] = map[string]int{
			"age": 18,
			"pwd": 8888,
		}
	} else {
		user[name]["pwd"] = 8888
	}
}

func main() {
	map1 := map[int]string{
		1: "hello",
		2: "world",
		3: "!",
	}
	fmt.Println(map1)
	//map依然是无序输出
	for key, v := range map1 {
		fmt.Println(key, v)
	}

	var map2 = make(map[int]string)
	fmt.Println(map2, len(map2))
	map2[1] = "h"
	map2[2] = "e"
	map2[3] = "l"
	fmt.Println(map2, len(map2))

	var monster = make([]map[string]string, 2)
	monster[0] = map[string]string{
		"age":  "500",
		"name": "牛魔王",
	}
	monster[1] = map[string]string{
		"age":  "350",
		"name": "玉兔精",
	}
	newMonster := map[string]string{
		"age":  "200",
		"name": "红孩儿",
	}
	//append动态添加切片元素
	monster = append(monster, newMonster)
	fmt.Println(monster)
	var mmap = make(map[string]map[string]int)
	fmt.Println(mmap)
	test1(mmap, "user1")
	//定义好之后，赋值value时不用再声明 :=，直接赋值=即可
	mmap["user2"] = map[string]int{
		"age": 15,
		"pwd": 1234,
	}
	fmt.Println(mmap)
	test1(mmap, "user2")
	fmt.Println(mmap)
}
